package com.iw.core.security.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.iw.core.orm.PageData;
import com.iw.core.security.entity.Resources;
import com.iw.core.security.entity.ResourcesGroup;
import com.iw.core.security.service.ResourcesGroupService;
import com.iw.core.security.service.ResourcesService;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/security/resources")
public class ResourcesAction {
	@Resource
	private ResourcesService resourcesService;

	@Resource
	private ResourcesGroupService resourcesGroupService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<Resources> pageData = new PageData<Resources>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = resourcesService.find(pageData);
		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		List<ResourcesGroup> groupList = resourcesGroupService.findAll();
		model.addAttribute("groupList", groupList);
		return "/security/resources/input";
	}


    @RequestMapping(value = "/repeat")
    @ResponseBody
    public boolean repeat(String name, Integer id) {
        try {
            name = name.trim();
            List<Resources> resourcesList = resourcesService.findList("name", name);
            if (resourcesList != null && !resourcesList.isEmpty()) {
                if (id != 0) {
                    Resources r = resourcesService.find(id);
                    if (!r.getName().equals(name)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		Resources resources = resourcesService.find(id);
		model.addAttribute(resources);
		List<ResourcesGroup> groupList = resourcesGroupService.findAll();
		model.addAttribute("groupList", groupList);
		return "/security/resources/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Resources resources) {
		if (resources.isNew()) {
			resourcesService.save(resources);
		} else {
			resourcesService.update(resources);
		}

		return "redirect:/security/resources/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		resourcesService.delete(id);
		return "redirect:/security/resources/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		resourcesService.visible(id);
		return "redirect:/security/resources/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		resourcesService.unVisible(id);
		return "redirect:/security/resources/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
